package com.jmes.web.service;

import com.jmes.web.dao.DaoManager;
import com.jmes.web.dao.FunctionCodeTableMapper;
import com.jmes.web.dao.RoleFunctionMapper;
import com.jmes.web.model.*;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 权限服务.
 * User: jinshunlong
 * Date: 13-4-14
 * Time: 下午2:17
 */
public class RoleService {
    private static Map<Integer,FunctionCodeTable> FunctionCodeTableMap = new HashMap<Integer,FunctionCodeTable>();

    /**
     * 加载功能表
     */
    private static void loadFunctionTables() {
        //把功能码表加载到内存
        List<FunctionCodeTable> functionCodeTables = findAllFunctions();
        for (FunctionCodeTable functionCodeTable : functionCodeTables){
            FunctionCodeTableMap.put(functionCodeTable.getId(),functionCodeTable);
        }
    }
    /**
     * 通过roleId获取他对应的功能点
     * @param roleId
     * @return
     */
    public static List<RoleFunction> getRoleFunctionByRoleId(final int roleId){
        List<RoleFunction> roleFunctions = null;
        roleFunctions = (List<RoleFunction>)DaoManager.execDao(new DaoManager.ExecDAO() {
            @Override
            public Object execDao(SqlSession sqlSession) {
                RoleFunctionMapper roleFunctionMapper = sqlSession.getMapper(RoleFunctionMapper.class);
                RoleFunctionExample roleFunctionExample = new RoleFunctionExample();
                roleFunctionExample.createCriteria().andIdEqualTo(roleId);
                return roleFunctionMapper.selectByExample(roleFunctionExample);
            }
        });
        return roleFunctions;
    }

    /**
     * 获取当前系统所有的功能表
     * @return
     */
    public static List<FunctionCodeTable> findAllFunctions(){
        List<FunctionCodeTable> functionCodeTables = null;
        functionCodeTables = (List<FunctionCodeTable>)DaoManager.execDao(new DaoManager.ExecDAO() {
            @Override
            public Object execDao(SqlSession sqlSession) {
                FunctionCodeTableMapper functionCodeTableMapper = sqlSession.getMapper(FunctionCodeTableMapper.class);
                return functionCodeTableMapper.selectByExample(new FunctionCodeTableExample());
            }
        });
        return functionCodeTables;
    }

    /**
     * 获取某个角色的功能表
     * @param roleId
     * @return
     */
    public static List<FunctionCodeTable> findUserFunctionsByRoleId(final int roleId){
        List<FunctionCodeTable> functionCodeTables = new ArrayList<FunctionCodeTable>();
        List<RoleFunction> roleFunctions = getRoleFunctionByRoleId(roleId);
        for (RoleFunction roleFunction : roleFunctions){
            if(FunctionCodeTableMap.containsKey(roleFunction.getFunctionId())){
                functionCodeTables.add(FunctionCodeTableMap.get(roleFunction.getFunctionId()));
            }
        }
        return functionCodeTables;
    }

    /**
     * 获取某个角色的功能表,以tree的方式返回
     * @param roleId
     * @return
     */
    public static List<FunctionTreeNode> findUserFunctionTreeByRoleId(final int roleId){
        List<FunctionCodeTable> functionCodeTables = findUserFunctionsByRoleId(roleId);
        return parseFunctionTree(functionCodeTables);
    }

    private static List<FunctionTreeNode> parseFunctionTree(List<FunctionCodeTable> functionCodeTables){
        if(functionCodeTables == null || functionCodeTables.size() == 0){
            return new ArrayList<FunctionTreeNode>();
        }
        List<FunctionTreeNode> root = new ArrayList<FunctionTreeNode>();
        for (FunctionCodeTable functionCodeTable : functionCodeTables){
            if(functionCodeTable.getParentId() == -1){
                FunctionTreeNode oneRoot = new FunctionTreeNode();
                oneRoot.setFunctionCodeTable(functionCodeTable);
                parseFunctionTreeInner(functionCodeTables,oneRoot);
                root.add(oneRoot);
            }
        }
        return root;
    }

    private static void parseFunctionTreeInner(List<FunctionCodeTable> functionCodeTables,FunctionTreeNode parent){
        int parentId = -1;
        if(parent != null){
            parentId = parent.getFunctionCodeTable().getId();
        }
        for(FunctionCodeTable functionCodeTable : functionCodeTables){
            if(functionCodeTable.getParentId() == parentId){
                FunctionTreeNode functionTreeNode = new FunctionTreeNode();
                functionTreeNode.setFunctionCodeTable(functionCodeTable);
                parent.getChildFunction().put(functionCodeTable.getId(),functionTreeNode);
                parseFunctionTreeInner(functionCodeTables, functionTreeNode);
            }
        }
    }

    /**
     * 初始化系统功能表
     */
    public static void setupFunctions(){
        List<FunctionCodeTable> functionCodeTables = (List<FunctionCodeTable>)DaoManager.execDao(new DaoManager.ExecDAO() {
            @Override
            public Object execDao(SqlSession sqlSession) {
                FunctionCodeTableMapper functionCodeTableMapper = sqlSession.getMapper(FunctionCodeTableMapper.class);
                List<FunctionCodeTable> functionCodeTables = functionCodeTableMapper.selectByExample(new FunctionCodeTableExample());
                return functionCodeTables;
            }
        });
        if(functionCodeTables == null || functionCodeTables.size() == 0){
            DaoManager.execDao(new DaoManager.ExecDAO() {
                @Override
                public Object execDao(SqlSession sqlSession) {
                    FunctionCodeTableMapper functionCodeTableMapper = sqlSession.getMapper(FunctionCodeTableMapper.class);


                    FunctionCodeTable userInfoSetting = new FunctionCodeTable();
                    userInfoSetting.setId(0);
                    userInfoSetting.setName("个人设定");
                    userInfoSetting.setUrl("");
                    userInfoSetting.setRemark("");
                    userInfoSetting.setParentId(-1);
                    functionCodeTableMapper.insertSelective(userInfoSetting);
                    return null;
                }
            },false);
            DaoManager.execDao(new DaoManager.ExecDAO() {
                @Override
                public Object execDao(SqlSession sqlSession) {
                    FunctionCodeTableMapper functionCodeTableMapper = sqlSession.getMapper(FunctionCodeTableMapper.class);


                    FunctionCodeTable userInfoSetting = new FunctionCodeTable();
                    userInfoSetting.setParentId(0);
                    userInfoSetting.setId(1);
                    userInfoSetting.setName("用户基本信息设置");
                    userInfoSetting.setUrl("/user_setting.zul");
                    userInfoSetting.setRemark("");
                    functionCodeTableMapper.insertSelective(userInfoSetting);
                    return null;
                }
            },false);
        }
        loadFunctionTables();
    }
}
